package monster.com.cvh.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.MotionEvent;


/**
 * @author: 13580
 * @data: 2018/10/22
 * @function:处理Edittext和RecyclerView滑动冲突事件的Edittext
 */

public class MyEditText extends android.support.v7.widget.AppCompatEditText {

    public Layout mLayout;
    public int paddingTop;
    public int paddingBottom;
    public int mHeight;
    public int mLayoutHeight;

    public MyEditText(Context context) {
        super(context);
        init();
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mLayout = getLayout();
        mLayoutHeight = mLayout.getHeight();
        paddingTop = getTotalPaddingTop();
        paddingBottom = getTotalPaddingBottom();
        mHeight = getHeight();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        getParent().requestDisallowInterceptTouchEvent(true);
        RecyclerView recyclerView = (RecyclerView) getParent().getParent();
        //如果edittext能滑动，则让Recycler不拦截
        if (event.getAction() == MotionEvent.ACTION_MOVE && canScrollVertically(-1) || canScrollVertically(1)) {
            recyclerView.requestDisallowInterceptTouchEvent(true);
        } else {
            recyclerView.requestDisallowInterceptTouchEvent(false);
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
        super.onScrollChanged(horiz, vert, oldHoriz, oldVert);
        //这里是滑动到底部和滑动（顶部只用计算vert的值是否为0就可以）
        int height = mLayoutHeight + paddingTop + paddingBottom - mHeight;//文本内容的高度
        RecyclerView recyclerView = (RecyclerView) getParent().getParent();
        if (vert < height && vert > 0) {
            recyclerView.requestDisallowInterceptTouchEvent(true);
        } else {
            recyclerView.requestDisallowInterceptTouchEvent(false);
        }
    }
}